<?php

namespace app\apishop\controller;

use app\admin\model\article\Atype;
use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 公告列表
 */
class Article extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\article\Article();
        $this->modelType = new Atype();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }


    /**
     *  类型列表
     * @return void
     */
    public function type_list()
    {
        try {
            $data = $this->modelType
                ->order('id desc')
                ->select();
            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


    /**
     *  文章列表
     * @return void
     */
    public function list()
    {
        $input = input();
        try {
            $pageArr = [
                'page' => 1,
                'page_limit' => 30,
            ];
            $where = [];
            if (!empty($input['article_type_id'])) {
                $where['article_type_id'] = $input['article_type_id'];
            }
            if (!empty($input['id'])) {
                $where['id'] = $input['id'];
            }
            if (!empty($input['keyWork'])) {
                $where['title'] = ['like', '%' . $input['keyWork'] . '%'];
            }
            $pageArr = array_merge($pageArr, $input);
            $data = $this->model->where($where)
                ->order('weigh desc')
                ->paginate([
                    'page' => $pageArr['page'] ?: 1,
                    'list_rows' => $pageArr['page_limit'] ?: 10
                ]);
            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }
}